<?php
/**
 * User: hufeng
 * Date: 2016/12/21
 * Desc: excel处理
 */
namespace app\common\util;
use Predis\Client;

class Redis{
    public $client;
    public function __construct(){
        $redisConf = config('redis.REDIS');
        $redisSentinels = $redisConf['REDISSENTINELS'];
        $redisOptions = $redisConf['REDISOPTIONS'];
        $this->client = new Client($redisSentinels,$redisOptions);
    }

    public function __destruct(){
        $this->client->quit();
    }

    /*单次获得锁资格*/
    public function getLock($key){
        if($this->client->setnx($key,1)){
            return true;
        }else{
            return false;
        }
    }
    /*循环获得锁资格,直至获取到*/
    public function getLockEnabel($key){
        if($this->client->setnx($key,1)){
            return true;
        }else{
            $this->getLockEnabel($key);
        }
        return true;
    }
    /*释放锁*/
    public function removeLock($key){
        $this->client->del([$key]);
    }

}